package com.zwk.views;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.DefaultComboBoxModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableColumn;

import com.zwk.dao.WKStuInfo;
import com.zwk.model.WKStdLoginOperator;

public class WKSeeStdMsgView {

	private static final long serialVersionUID = 1L;
	private static final int DEFAULT_WIDTH = 500;
	private static final int DEFAULT_HEIGHT = 380;
	public static JFrame frame;
	JFrame parentFrame;
	public WKSeeStdMsgView(String id) {
		frame = new JFrame();
		frame.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
		frame.setTitle("查看学生信息");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		Toolkit kit = Toolkit.getDefaultToolkit();
		Dimension screenSize = kit.getScreenSize();
		int screenWidthpx = screenSize.width;
		int screenHeightpx = screenSize.height;
		frame.setLocation(screenWidthpx / 3, screenHeightpx / 3);
		frame.setLocationByPlatform(false);
		
		Image image = new ImageIcon(this.getClass().getResource("/WKImage/top1.jpg")).getImage();
		frame.setIconImage(image);
		WKSeeStdMsgPanel loginPanel = new WKSeeStdMsgPanel(id);
		frame.setContentPane(loginPanel);
		frame.setLayout(null);
		frame.setVisible(true);
		
	}
	private class WKSeeStdMsgPanel extends JPanel {
		/**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		JPanel infoPanel;
		JLabel wayLabel;
		JComboBox wayComboBox;
		
		JPanel selectPanel;
		JLabel selectLabel;
		JTextField selectTextField;
		JButton selectButton;
		
	    JPanel buttonPanel;
	    JButton backButton;
	    JButton refreshButton;
	    
	    JPanel tablePanel;
	    JTable reasonTable;
	    
	    public WKSeeStdMsgPanel(final String id) {
	
	    	WKStdLoginOperator OP = new WKStdLoginOperator();
	    	List<WKStuInfo> students = OP.getAllInfo();
	    	//查询界面
	    	infoPanel = new JPanel();
	    	infoPanel.setBounds(0, 0, 480, 40);
	    	infoPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
	    	
	    	wayLabel = new JLabel("请选择检查方式   ");
	    	DefaultComboBoxModel way = new DefaultComboBoxModel();
		    way.addElement("按姓名查询");
		    way.addElement("按学号查询");
		    way.addElement("按专业查询");
		    way.addElement("按教学班查询");
		    wayComboBox = new JComboBox(way);
	    	wayComboBox.setSelectedIndex(0);
	    	
	    	selectPanel = new JPanel();
	    	selectPanel.setBounds(0, 40, 480, 40);
	    	selectPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
	    	selectLabel = new JLabel("请输入查询关键字");
	    	selectTextField = new JTextField(20);
	    	selectButton = new JButton("查询");
	    	
	    	infoPanel.add(wayLabel);
	    	infoPanel.add(wayComboBox);
	    	selectPanel.add(selectLabel);
	    	selectPanel.add(selectTextField);
	    	selectPanel.add(selectButton);
	    	add(infoPanel, BorderLayout.CENTER);
	    	add(selectPanel);
	    	//数据展示
	    	tablePanel = new JPanel();
	    	tablePanel.setBounds(0, 80, 500, 240);
	    	tablePanel.setLayout(new FlowLayout(FlowLayout.CENTER));
	        tablePanel.setBorder(BorderFactory.createEtchedBorder());
	        
	    	String[] headers = {"姓名", "学号", "所属学院", "专业"};
	    	Object[][] cellData = (new Object[][]{
	    		{students.get(0).getName(), students.get(0).getId(), students.get(0).getCollege(), students.get(0).getMajor()}, 
	    		{students.get(1).getName(), students.get(1).getId(), students.get(1).getCollege(), students.get(1).getMajor()},
	    			});
	    	DefaultTableModel model = new DefaultTableModel(cellData, headers) {
	    		public boolean isCellEditable(int row, int column) {
	    			return false;
	    		}
	    	};
	    	reasonTable = new JTable(model);
	    	reasonTable.setAutoResizeMode(JTable.AUTO_RESIZE_SUBSEQUENT_COLUMNS);
	    	reasonTable.setPreferredScrollableViewportSize(new Dimension(480, 320));
	    	reasonTable.setShowGrid(true);
	    	reasonTable.setShowHorizontalLines(true);
	    	reasonTable.setShowVerticalLines(true);
	    	TableColumn firsetColumn = reasonTable.getColumnModel().getColumn(0);
//	    	firsetColumn.setPreferredWidth(110);
//	    	firsetColumn.setMaxWidth(110);
//	    	firsetColumn.setMinWidth(110);
	    	DefaultTableModel tableModel = (DefaultTableModel)reasonTable.getModel();
	    	int count = 4;
	    	tableModel.setColumnCount(count);
	    	reasonTable.setRowHeight(20);
	    	tableModel.setRowCount(2);
	    	JScrollPane scrollPane = new JScrollPane(reasonTable);
	    	tablePanel.add(scrollPane, BorderLayout.CENTER);
	    	add(tablePanel);
	 
	    
	  //按钮部分
    	buttonPanel = new JPanel();
    	buttonPanel.setBounds(0, 320, 500, 50);
//    	buttonPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
    	buttonPanel.setBorder(BorderFactory.createEtchedBorder());
    	backButton = new JButton("返回上级菜单");
    	refreshButton = new JButton("重新开始查询");
    	buttonPanel.add(refreshButton);
    	buttonPanel.add(backButton);
    	backButton.addActionListener(new ActionListener() {
    		@Override
    		public void actionPerformed(ActionEvent e) {
    			WKTchFuncPortalFrame stdView = new WKTchFuncPortalFrame(id);
    			frame.dispose();
    		}
    	});
    	add(buttonPanel, BorderLayout.SOUTH);
	    }
	}
	
}
